package cn.mooncode.basketballManage.controller;


import cn.mooncode.basketballManage.pojo.*;
import cn.mooncode.basketballManage.service.IPlayerinfoService;
import cn.mooncode.basketballManage.service.IUserPlayerinfoService;
import cn.mooncode.basketballManage.utils.UserUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jwz
 * @since 2021-03-28
 */
@RestController
@RequestMapping("/playerinfo")
public class PlayerinfoController {

    @Autowired
    private IPlayerinfoService playerinfoService;
    @Autowired
    private IUserPlayerinfoService userPlayerinfoService;

    @ApiOperation(value = "添加球员信息")
    @PostMapping("/")
    @Transactional
    public RespBean addPlayer(@RequestBody Playerinfo playerinfo) {
        playerinfo.setMatch_assist(0);
        playerinfo.setMatch_foul(0);
        playerinfo.setMatch_goals(0);
        playerinfo.setMatch_mistake(0);
        playerinfo.setMatch_steal(0);
        playerinfo.setTrain_assist(0);
        playerinfo.setTrain_foul(0);
        playerinfo.setTrain_goals(0);
        playerinfo.setTrain_mistake(0);
        playerinfo.setTrain_steal(0);
        if (playerinfoService.save(playerinfo)) {
            Integer uid = UserUtils.getUser().getId();
            Integer pid = playerinfo.getId();
            UserPlayerinfo userPlayerinfo = new UserPlayerinfo();
            userPlayerinfo.setPid(pid);
            userPlayerinfo.setUid(uid);
            if (userPlayerinfoService.save(userPlayerinfo)) {
                return RespBean.success("添加成功！");
            }
        }
        return RespBean.error("添加失败！");
    }

    @ApiOperation(value = "查看所有球员信息")
    @GetMapping("/")
    public RespPageBean getAllPlayerInfo(@RequestParam(defaultValue = "1") Integer currentPage,
                                         @RequestParam(defaultValue = "2") Integer size, Playerinfo playerinfo) {
        return playerinfoService.getAllPlayerInfo(currentPage, size, playerinfo);
    }

    @ApiOperation(value = "获取当前球员信息")
    @GetMapping("/currentPlayer")
    public Playerinfo getCurrentPlayerInfo() {
        Integer id = UserUtils.getUser().getId();
        return playerinfoService.getCurrentPlayerInfo(id);
    }


    @ApiOperation(value = "修改球员信息")
    @PutMapping("/")
    public RespBean updatePlayerInfo(@RequestBody Playerinfo playerinfo) {
        if (playerinfoService.updateById(playerinfo)) {
            return RespBean.success("修改成功！");
        } else {
            return RespBean.error("修改失败！");
        }
    }

    @ApiOperation(value = "修改球员成绩")
    @PutMapping("/setScore")
    public RespBean updateScores(@RequestBody ScoreParam scoreParam) {
        return playerinfoService.updateScores(scoreParam);
    }

    @ApiOperation(value = "删除球员")
    @DeleteMapping("/{id}")
    @Transactional
    public RespBean deletePlayerInfo(@PathVariable Integer id) {
        if (playerinfoService.removeById(id)) {
            if (userPlayerinfoService.remove(new QueryWrapper<UserPlayerinfo>().eq("pid", id))) {
                return RespBean.success("删除成功！");
            }
        }
        return RespBean.error("删除失败！");

    }

}
